package cn.com.doone.common.uc.oauth.shiro;

import java.io.Serializable;
import java.util.List;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.ValidatingSession;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import com.sun.org.apache.xml.internal.security.utils.Base64;

import cn.com.doone.common.uc.utils.SerializeUtils;

public class DooneSessionDao extends CachingSessionDAO {

	@Autowired
    protected JdbcTemplate jdbcTemplate;
	
	protected Serializable doCreate(Session session) {
	    Serializable sessionId = generateSessionId(session);
	    assignSessionId(session, sessionId);
	    String sql = "insert into sessions(id, session) values(?,?)";
	    jdbcTemplate.update(sql, sessionId, Base64.encode(SerializeUtils.serialize(session)));
	    return session.getId();
	}

	protected void doUpdate(Session session) {
	    if (session instanceof ValidatingSession && !((ValidatingSession) session).isValid()) {
	        return; //如果会话过期/停止 没必要再更新了
	    }
	    
	    String sql = "update sessions set session=? where id=?";
	    jdbcTemplate.update(sql, Base64.encode(SerializeUtils.serialize(session)), session.getId());
	}
	protected void doDelete(Session session) {
	    String sql = "delete from sessions where id=?";
	    jdbcTemplate.update(sql, session.getId());
	}
	protected Session doReadSession(Serializable sessionId) {
	    String sql = "select session from sessions where id=?";
	    List <String> sessionStrList = jdbcTemplate.queryForList(sql, String.class, sessionId);
	    if (sessionStrList.size() == 0) return null;
	    return (Session) SerializeUtils.deserialize(sessionStrList.get(0).getBytes());
	}

}
